/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2009-4-27 
 * LastModified: 2009-4-27
 */
package edu.hit.pact.pgse.crawler.util;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;

//import org.apache.http.client.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Meteor
 *
 */
public class CustomHttpClient {

	private Log logger;
	
	private HttpClient client;
	
	public CustomHttpClient(int maxPerHostConnection, int maxTotalConnection, int connectTimeout) {
		this(LogFactory.getLog(CustomHttpClient.class), maxPerHostConnection, maxTotalConnection, connectTimeout);
	}
	
	public CustomHttpClient(Log logger, int maxPerHostConnection, int maxTotalConnection, int connectTimeout) {
		this.logger = logger;
		
		MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

		/*
		 * default max connections per host is 2.
		 */
		//logger.info("set max connections per host :" + maxPerHostConnection);
		connectionManager.getParams().setDefaultMaxConnectionsPerHost(maxPerHostConnection);			

		/*
		 * default max Total connections is 20.
		 */
		//logger.info("set max total connections :" + maxTotalConnection);
		connectionManager.getParams().setMaxTotalConnections(maxTotalConnection);
		
		//set httpclient connection timeout
		if (connectTimeout > 0) {
			logger.info("set connection timeout :" + connectTimeout + " seconds");
			connectionManager.getParams().setConnectionTimeout(connectTimeout * 1000);	
		}

		client = new HttpClient(connectionManager);		
	}
	
	public void setProxy(String proxyIp, int proxyPort) {
		setProxy(proxyIp, proxyPort, null, null);
	}
	
	public void setProxy(String proxyIp, int proxyPort, String proxyUser, String proxyPass) {
		logger.info("set proxy ip :" + proxyIp + " and port :" + proxyPort);
		client.getHostConfiguration().setProxy(proxyIp, proxyPort);

		if (proxyUser == null) {
			return;
		}
		
		/*
		 * important!!!
		 * set httpclient AuthenticationPreemptive is true, or else you will receive fail message.
		 */
		client.getParams().setAuthenticationPreemptive(true);
		
		logger.info("set proxy user :" + proxyUser + " and proxy pass :" + proxyPass);
		UsernamePasswordCredentials creds = new UsernamePasswordCredentials(proxyUser, proxyPass);
		client.getState().setProxyCredentials(AuthScope.ANY, creds);		
	}
	
	public int executeMethod(HttpMethod method) throws HttpException, IOException {
		return this.client.executeMethod(method);
	}
}
